package com.qc.bus.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qc.bus.dao.BusProviderDao;
import com.qc.bus.entity.BusProvider;
import com.qc.bus.service.BusProviderService;
import com.qc.bus.vo.ProviderVo;
import com.qc.sys.common.Constant;
import com.qc.sys.common.DataGridView;
import com.qc.sys.common.ResultObj;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
 * (BusProvider)表服务实现类
 *
 * @author AltmanQccc
 * @since 2020-09-08 23:11:01
 */
@Service
@EnableCaching
public class BusProviderServiceImpl extends ServiceImpl<BusProviderDao, BusProvider> implements BusProviderService {

    @Autowired
    private BusProviderDao providerDao;

    @Override
    public DataGridView queryAllProvider(ProviderVo providerVo) {
        IPage<BusProvider> page = new Page(providerVo.getPage(),providerVo.getLimit());
        QueryWrapper<BusProvider> qw = new QueryWrapper<>();
        qw.eq(providerVo.getAvailable()!=null,"available",providerVo.getAvailable());
        qw.like(StringUtils.isNotBlank(providerVo.getProvidername()),"providername",providerVo.getProvidername());
        qw.like(StringUtils.isNotBlank(providerVo.getConnectionperson()),"connectionperson",providerVo.getConnectionperson());
        if(StringUtils.isNotBlank(providerVo.getPhone())) {
            qw.and(new Consumer<QueryWrapper<BusProvider>>() {
                @Override
                public void accept(QueryWrapper<BusProvider> busProviderQueryWrapper) {
                    qw.like(StringUtils.isNotBlank(providerVo.getPhone()), "phone", providerVo.getPhone())
                            .or().like(StringUtils.isNotBlank(providerVo.getPhone()), "telephone", providerVo.getPhone());
                }
            });
        }
        this.providerDao.selectPage(page,qw);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    @CachePut(cacheNames = "BusProviderServiceImpl",key = "#result.id")
    @Override
    public BusProvider InsertProvider(BusProvider provider) {
        this.providerDao.insert(provider);
        return provider;
    }

    @CachePut(cacheNames = "BusProviderServiceImpl",key = "#result.id")
    @Override
    public BusProvider UpdateProvider(BusProvider provider) {
        BusProvider busProvider = this.providerDao.selectById(provider.getId());
        BeanUtil.copyProperties(provider,busProvider, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        this.providerDao.updateById(busProvider);
        return busProvider;
    }

    @Override
    public ResultObj batchDeleteProvider(Integer[] ids) {
        try {
            //1.对ids进行判断，是否为空，是否长度大于0
            if (null!=ids&&ids.length>0){
                //2.对ids进行遍历删除
                List<Integer> idList = new ArrayList<>();
                for (Integer id : ids) {
                    idList.add(id);
                }
                this.providerDao.deleteBatchIds(idList);
                return ResultObj.DELETE_SUCCESS;
            } else {
                //3.false执行 {code:-1,msg:"传入ids异常"}
                return new ResultObj(-1,"传入ids异常");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    @Override
    public DataGridView queryAllAvailableProvider() {
        QueryWrapper<BusProvider> qw = new QueryWrapper<>();
        qw.eq("available", Constant.USER_AVAILABLE_TRUE);
        List<BusProvider> providers = this.providerDao.selectList(qw);
        return new DataGridView(providers);
    }

    @CacheEvict(cacheNames = "BusProviderServiceImpl",key = "#id")
    @Override
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

    @Cacheable(cacheNames = "BusProviderServiceImpl",key = "#id")
    @Override
    public BusProvider getById(Serializable id) {
        return super.getById(id);
    }
}